package com.ln.dto;

import java.util.HashMap;

import com.ln.common.PagerCompute;
import com.ln.exception.DTOException;

public class ParamsMap extends HashMap<String, Object> {

	public enum OrderBy {
		asc, desc
	}

	private static final long serialVersionUID = 4240671399415517892L;

	private ParamsMap() {
	}

	private ParamsMap(int initialCapacity) {
		super(initialCapacity);
	}

	public static ParamsMap create() {
		return create(0);
	}

	public static ParamsMap create(int initialCapacity) {
		return new ParamsMap(initialCapacity);
	}

	public static ParamsMap create(int pageIndex, int pageSize) {
		return create(pageIndex, pageSize, null);
	}

	public static ParamsMap create(int pageIndex, int pageSize, String orderBy) {
		return new ParamsMap().init(pageIndex, pageSize, orderBy);
	}

	private ParamsMap init(int pageIndex, int pageSize, String orderBy) {
		this.put("pageIndex", pageIndex);
		this.put("pageSize", pageSize);
		if (orderBy != null) {
			this.put("orderBy", orderBy);
		}
		return this;
	}

	public ParamsMap add(String key, Object value) {
		super.put(key, value);
		return this;
	}

	public String getString(Object key) {
		Object o = super.get(key);
		if (o == null || "".equals(o)) {
			throwsDTOException(key);
		}
		return o.toString();
	}

	public int getInt(Object key) {
		Object o = super.get(key);
		if (o == null || "".equals(o)) {
			throwsDTOException(key);
		}
		return Integer.parseInt(o.toString());
	}

	public long getLong(Object key) {
		Object o = super.get(key);
		if (o == null || "".equals(o)) {
			throwsDTOException(key);
		}
		return Long.parseLong(o.toString());
	}

	private void throwsDTOException(Object key) {
		try {
			throw new DTOException(getClass().getSimpleName() + " [\"" + key
					+ "\"] not found.");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
